import game.tcturtle
from PIL import ImageTk,Image
import tkinter as tk
import math
import random
from game.obj.missile import Missile

def class_name():
    return "xuanfutuzhi"

class CombatTurtle(game.tcturtle.TurtleParent):
    def __init__(self, game, name=class_name(), col="black",coords=(200,710), heading=0):
        global missile_image
        missile_image = ImageTk.PhotoImage(file="./logo2.jpg")
        self.image = ImageTk.PhotoImage(file = "./logo1.jpg")
        super(CombatTurtle, self).__init__(game, name=class_name(), col="black",coords=(200, 200), heading=0)
        self.canvas = game.canvas


    def class_name():
        return "xuanfutuzhi"

    def class_desc():
        return "The cycle life and death continues.We will live, they will die"

    def class_shape():
        return 0

    def setup(self):
        "初始化设定"
        self.re_cont = 0
        self.cont = 10
        self.free_cont = 0
        self.pursuit_range = 0.8 * self.missile_range
        self.speed = self.max_speed
        (self.head, self.wait) = self.rerandomize()

    def step(self):
        self.wait -= 1

        #预瞄
        self.focase_x, self.focase_y = self.focase_other_position()
        azimuth = self.Calculation_azimuth_angle(self.focase_x, self.focase_y)
        if self.wait <= 0:
            (self.head, self.wait) = self.rerandomize()
        direct = self.heading_towards((self.other_x, self.other_y))

        if abs(direct - self.heading) > 120:
            self.turn_towards()
        else:
            if (self.can_shoot and self.line_of_sight() and self.distance() < self.pursuit_range):
                #射击规则
                if abs(self.relative_heading_towards()) <= 3:
                    self.shoot()
                    self.cont = 10
                    self.forward()
                    self.cont = 10

            if (self.distance() <= self.pursuit_range and self.line_of_sight()):
                #防止撞墙
                if self.re_cont == 0:
                    if self.x < 50 or self.x > 750 or self.y < 50 or self.y > 700:
                        self.re_cont = 200
                    if self.distance() < (15 + random.randint(-5,5)) * self.missile_radius:
                        self.backward()
                        self.cont -= 1
                        #扭身
                        if self.cont > 5:
                            self.right((azimuth - self.heading) < 0 and -1 or 1)
                        else:
                            self.right(-1 * (azimuth - self.heading) < 0 and -1 or 1)
                    else:
                        self.forward()
                else:
                    self.turn_towards()
                    self.forward()
                    if self.x > 200 and self.x < 600 and self.y > 200 and self.y < 600:
                        self.re_cont = 0
            else:
                self.turn_towards()
                self.forward()


    def Calculation_azimuth_angle(self, focase_x, focase_y):
        #预瞄方向计算
            if focase_y - self.y == 0:
                return 45
            else:
                angle = math.atan((focase_x - self.x) / (focase_y - self.y)) * 180 / math.pi
                if angle < 0:
                    azimuth = angle + ((focase_x - self.x) < 0 and -1 or 0) * ((focase_y - self.y) < 0 and -1 or 1) * -180
                else:
                    azimuth = angle + (((focase_x - self.x) < 0 and (focase_y - self.y) < 0) and -1 or 1) * 90 - 90
            return azimuth


    def focase_other_position(self):
        #预瞄位置计算
        rand_angle = random.randint(-1,1)
        focase_x = self.other_x + (math.cos(self.other_heading) * self.distance() / self.missile_speed * 0.1) * rand_angle
        focase_y = self.other_y + (math.sin(self.other_heading) * self.distance() / self.missile_speed * 0.1) * rand_angle
        return focase_x, focase_y


    def rerandomize(self):
        rh = random.randrange(-179, 181)
        rt = random.randrange(5, 31)
        return (rh, rt)